package com.qqtech.qquan.mall.controller;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.mall.enums.MallProductSpecStatusDictEnum;
import com.qqtech.qquan.mall.model.MallProduct;
import com.qqtech.qquan.mall.model.MallProductSpec;
import com.qqtech.qquan.mall.model.MallProductSpecQuery;
import com.qqtech.qquan.mall.model.MallProductSpecVo;
import com.qqtech.qquan.mall.service.MallProductService;
import com.qqtech.qquan.mall.service.MallProductSpecService;
import com.qqtech.qquan.sys.controller.BaseAdminController;

@Controller
@RequestMapping(value = "/mall/product/spec")
public class MallProductSpecController extends BaseAdminController {
	@Resource
	private MallProductSpecService mallProductSpecService;
    @Resource 
    private MallProductService mallProductService;

	@Override
	protected BaseService<MallProductSpec> getBaseService() {
		return mallProductSpecService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(MallProductSpecQuery query, HttpServletRequest request,
			HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(),
				new PageOrder(Direction.DESC, "id"));
		Page<MallProductSpecVo> page = mallProductSpecService.queryPageList(query,pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	@RequestMapping(value = "/doDelete", method = RequestMethod.POST)
	@ResponseBody
	public Result doUpdate(@RequestBody MallProductSpec mallProductSpec) {
		Result ret = new Result();
		MallProductSpec old = null;
		if (mallProductSpec == null || mallProductSpec.getId() == null) {
			ret.addErr("参数不能为空");
		} else {
			old = mallProductSpecService.getById(mallProductSpec.getId());
			if (old == null) {
				ret.addErr("产品规格不存在");
			}
		}
		List<MallProductSpecVo> specLst = mallProductSpecService.queryListByProductId(old.getProductId());
		if(specLst == null){
			ret.addErr("商品规格不存在");
			return ret;
		}
		if(specLst.size() == 1){
			ret.addErr("目前商品规格仅为一个，无法删除");
			return ret;
		}
		if (ret.checkForm()) {
			Timestamp now = TimeUtil.now();
			MallProductSpec entity = new MallProductSpec();
			entity.setId(mallProductSpec.getId());
			entity.setStatus(MallProductSpecStatusDictEnum.DELETE.getCode());
			entity.setUpdAt(now);
			mallProductSpecService.updateById(entity);
		}
		//取规格价格最小值，更新商品表价格字段
		Integer totalStock = 0;
		BigDecimal minPrice = BigDecimal.ZERO;
		BigDecimal currentPrice = BigDecimal.ZERO;
		for(int i=0; i<specLst.size(); i++){
			MallProductSpecVo specVo = specLst.get(i);
			if(mallProductSpec.getId().equals(specVo.getId())){
				continue;
			}
			currentPrice = specVo.getPrice();
			if(currentPrice == null){
				ret.addErr("商品规格价格为空");
				return ret;
			}
			if(i > 0){
				if(minPrice.compareTo(currentPrice) == 1){//取最小值
					minPrice = currentPrice;
				}
			}else{
				minPrice = currentPrice;
			}
			totalStock += specVo.getStock(); 
		}
		MallProduct updateEntity = new MallProduct();
		updateEntity.setId(old.getProductId());
		updateEntity.setPrice(minPrice);
		updateEntity.setStock(totalStock);
		mallProductService.updateById(updateEntity);
		return ret;
	}
}
